<?php
require_once("../connection.php");

$gpid = $_POST['gpid'];
$fbid = $_POST['fbid'];
$type = $_POST['type'];

if($type == "")
{

$temp = mysql_query("SELECT * FROM site_requests WHERE `fbid`='$fbid' AND `gpid`='$gpid'");
if(mysql_numrows($temp) == 0)
{
$res = mysql_query("INSERT INTO site_requests (`gpid`,`fbid`,`status`) VALUES ('$gpid','$fbid','0')");
if(!$res) echo mysql_error();
$id = mysql_insert_id();
}
else
{
$res = mysql_query("UPDATE site_requests SET `status`='0' WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
if(!$res) echo mysql_error();
$id = mysql_result($temp,0,'id');
}

echo "id_request_".$id;
}

// this is when the group is public and the user does not require any permission to join the circle.
else if($type == "public")
{
	
// adding user to the requests table with status 1 just to keep track
$temp2 = mysql_query("INSERT INTO site_requests (`gpid`,`fbid`,`status`) VALUES ('$gpid','$fbid','1')");
$id = mysql_insert_id();

// check if the user was already a member of this group before and was deleted. if yes, then just change the del parameter to 0
$temp1 = mysql_query("SELECT * FROM site_group_members WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
if(mysql_numrows($temp1)==0){
	$res1 = mysql_query("INSERT INTO site_group_members (`gpid`,`fbid`,`mod`,`del`) VALUES('$gpid','$fbid','0','0')");
	if(!$res1) echo mysql_error();
}
else{
	$res1 = mysql_query("UPDATE site_group_members SET `del`='0' WHERE `gpid`='$gpid' AND `fbid`='$fbid'");
	if(!$res1) echo mysql_error();
	
}
echo "id_join_".$id;

}

?>